package com.scedu.handler;

import com.scedu.exception.CommonException;
import com.scedu.result.CodeEnum;
import com.scedu.result.ResultBody;
import com.scedu.result.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalErrorHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object handleException(Exception e, HttpServletRequest request) {
        if (e instanceof CommonException) {
            CommonException commone = (CommonException)e;
            return ResultBody.failed(commone.getCode(), commone.getMsg());
        }else{
            return ResultBody.failed(ResultCode.FAILED.getCode(),e.getMessage());
        }

    }

}